package com.controller.api;

import com.biz.CustomerFavoriteBiz;
import com.biz.HotelBiz;
import com.biz.HotelPhotoBiz;
import com.controller.backend.BaseController;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.cond.BaseCond;
import com.entity.model.CustomerFavorite;
import com.entity.model.Hotel;
import com.entity.model.HotelPhoto;
import com.entity.vo.HotelVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;

import java.util.List;


/**
 * Created by jc on 2019/11/27.
 */
@RestController("apiCustomerFavoriteController")
@RequestMapping("/api/customerFavorite")
public class CustomerFavoriteController extends BaseController<CustomerFavorite> {
    @Autowired
    private CustomerFavoriteBiz customerFavoriteBiz;

    @Autowired
    private HotelBiz hotelBiz;
    @Autowired
    private HotelPhotoBiz hotelPhotoBiz;

    // 我的收藏列表
    @RequestMapping(value = "/favoriteList")
    public Result favoriteList(Integer customerId, BaseCond bcond) {
        PageHelper.startPage(bcond.getPageNum(), bcond.getPageSize(), false);
        PageHelper.orderBy(bcond.getOrderBy());
        List<HotelVO> hotels = hotelBiz.findCustomerFavoriteHotelList(customerId);
        for (Integer i = 0; i < hotels.size(); i++) {
            Condition condition = new Condition(HotelPhoto.class);
            condition.orderBy("id").asc();
            condition.createCriteria().andEqualTo("hotelId", hotels.get(i).getId());
            List<HotelPhoto> hotelPhotoList = hotelPhotoBiz.findByCondition(condition);
            if(hotelPhotoList.size()>0) {
                hotels.get(i).setHotelPhoto(hotelPhotoList.get(0).getUrl());
            }
        }
        return ResultGenerator.genSuccessResult(hotels);
    }

    // type=1:收藏 2:取消收藏
    @RequestMapping(value = "/favorite")
    public Result favorite(@RequestParam Integer customerId, @RequestParam Integer hotelId, @RequestParam Integer type) {
        if (customerId == null) {
            return ResultGenerator.genFailResult("用户编号不能为空");
        }
        if (hotelId == null) {
            return ResultGenerator.genFailResult("民宿编号不能为空");
        }
        if (type == null) {
            return ResultGenerator.genFailResult("收藏类型错误");
        }
        if (type == 1) {
            CustomerFavorite customerFavorite = new CustomerFavorite();
            customerFavorite.setCustomerId(customerId);
            customerFavorite.setHotelId(hotelId);
            customerFavoriteBiz.save(customerFavorite);
        } else if (type == 2) {
            Condition cond = new Condition(CustomerFavorite.class);
            cond.createCriteria().andEqualTo("customerId", customerId).andEqualTo("hotelId", hotelId);
            List<CustomerFavorite> customerFavorites = customerFavoriteBiz.findByCondition(cond);
            if (customerFavorites.size() > 0) {
                customerFavoriteBiz.deleteById(customerFavorites.get(0).getId());
            }
        }
        return ResultGenerator.genSuccessResult();
    }
}
